package com.pa165.languageschoolpresentation.rest;

import com.pa165.languageschoolpersistence.Exceptions.IllegalArgumentPersistenceException;
import com.pa165.languageschoolpersistence.Exceptions.ServiceFailureException;
import com.pa165.languageschoolservice.DTO.CourseDTO;
import com.pa165.languageschoolservice.DTO.LectureDTO;
import com.pa165.languageschoolservice.service.CourseService;
import com.pa165.languageschoolservice.service.LectureService;
import java.util.List;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Tomas Hruby
 */
@Component
@Path("/lectures")
public class LectureRestService {

    @Autowired
    private LectureService lectureService;
    
    @Autowired
    private CourseService courseService;

    public LectureService getLectureService() {
        return lectureService;
    }

    public void setLectureService(LectureService lectureService) {
        this.lectureService = lectureService;
    }

    
      public CourseService getCourseService() {
        return courseService;
    }

    public void setCourseService(CourseService courseService) {
        this.courseService = courseService;
    }
    
    

    @GET
    @Path("/getLecture")
    @Produces(MediaType.APPLICATION_JSON)
    public LectureDTO get(@QueryParam("id") Long id) {
        LectureDTO lecture;

        try {
            lecture = lectureService.get(id);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        if (lecture == null) {
            // 404
            throw new WebApplicationException(
                    Response.status(Response.Status.NOT_FOUND)
                    .entity("Lecture with id " + id + " wasn't found")
                    .build());
        }
        return lecture;
    }

    @GET
    @Path("/getAllLectures")
    @Produces(MediaType.APPLICATION_JSON)
    public List<LectureDTO> getAll() {
      List<LectureDTO>  list =  lectureService.getAll();
       if(list != null){
            return list;
        } else {
             // 404
            throw new WebApplicationException(
                    Response.status(Response.Status.NOT_FOUND)
                    .entity("getAll return null list")
                    .build());
        }        
    }

    
    @GET
    @Path("/getLecturesByCourse")
    @Produces(MediaType.APPLICATION_JSON)
    public List<LectureDTO> getLecturesByCourse(@QueryParam("id") Long id) {
         CourseDTO course;

        try {
            course = courseService.get(id);
        } catch (IllegalArgumentPersistenceException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        if (course == null) {
            // 404
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }
         List<LectureDTO>  list = null;
        try {
        
             list =  lectureService.getLectures(course);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        } 
       if(list != null){
            return list;
        } else {
             // 404
            throw new WebApplicationException(
                    Response.status(Response.Status.NOT_FOUND)
                    .entity("getLecturesByCourse return null list")
                    .build());
        }        
    }
    
    
    
 

    @POST
    @Path("/createLecture")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response add(LectureDTO lecture) {
        try {
            lectureService.add(lecture);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        String result = "Lecture created : " + lecture;
        // 200
        return Response.ok(result).build();
    }

    @POST
    @Path("/updateLecture")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response modify(LectureDTO lecture) {
        try {
            lectureService.modify(lecture);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        String result = "Lecture updated : " + lecture;
        // 200
        return Response.ok(result).build();
    }

    @DELETE
    @Path("/deleteLecture")
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteLecture(@QueryParam("id") Long id) {
        LectureDTO lecture;

        try {
            lecture = lectureService.get(id);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        if (lecture == null) {
            // 404
            throw new WebApplicationException(Response.Status.NOT_FOUND);
        }

        try {
            lectureService.remove(lecture);
        } catch (ServiceFailureException ex) {
            // 400
            throw new WebApplicationException(Response.Status.BAD_REQUEST);
        }

        String result = "Lecture removed : " + lecture;
        // 200
        return Response.ok(result).build();
    }
}
